package com.bluenave.crm.core;


import com.bluenave.crm.core.domain.*;
import org.hibernate.*;
import org.hibernate.cfg.AnnotationConfiguration;

import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: aleksanderbrancewicz
 * Date: 7/24/11
 * Time: 12:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class ApplicationStart {


    public static void main(String args[]){


         List<Person> persons = getSession().createQuery("from Person").list();
         System.out.println("found " + persons.size() + " items");

        Iterator i = persons.iterator();
        while (i.hasNext()){
            Person person = (Person)i.next();
            System.out.println("Person has an contact id: " + person.getId()) ;
        }


        Session session = getSession();

        try{
            session.beginTransaction();
            Company company = new Company("bell","they make phones");
            CompanyContact companyContact = new CompanyContact(company,"bell@bell.com",true,null);
            session.save(companyContact);
            session.save(company);
            session.getTransaction().commit();

        }catch (HibernateException e){
            Transaction tx = session.getTransaction();
            e.printStackTrace();
            if (tx.isActive()){
                tx.rollback();
            }
        }finally {
            session.close();
        }



        session = getSession();
        try{
            session.beginTransaction();
            Person person = new Person("Antona","Van Zant","F");
            PersonContact personContact = new PersonContact(person,"ant@ant.nl",true,null);
            session.save(person);
            session.save(personContact);
            session.getTransaction().commit();

        }catch (HibernateException e){
            Transaction tx = session.getTransaction();
            e.printStackTrace();
            if (tx.isActive()){
                tx.rollback();
            }
        }finally {
            session.close();
        }

         session = getSession();
        try{
            session.beginTransaction();
            Person person = new Person("Ludwig","Van Bethowen","M");
            Company company = new Company("Sony BMI","Music for masses");
            person.setCompany(company);
            session.save(company);
            session.save(person);
            session.getTransaction().commit();

        }catch (HibernateException e){
            Transaction tx = session.getTransaction();
            e.printStackTrace();
            if (tx.isActive()){
                tx.rollback();
            }
        }finally {
            session.close();
        }


    }


private static final SessionFactory sessionFactory;

    static {

        try {

            sessionFactory = new AnnotationConfiguration()

                    .configure().buildSessionFactory();

        } catch (Throwable ex) {

            ex.printStackTrace();
            // Log exception!

            throw new ExceptionInInitializerError(ex);

        }

    }


    public static Session getSession()

            throws HibernateException {

        return sessionFactory.openSession();

    }



}
